import Mock from 'mockjs';
import { Student } from './Student';

const mockTemplate = {
  id: '@integer(0, 100)',
  studentNo: '@string(5, 8)',
  name: '@string(5, 50)',
  gender: '@string(1, 1)',
  age: '@integer(0, 100)',
  className: '@string(5, 10)',
  major: '@string(5, 50)',
  grade: '@string(4, 4)',
  phone: '@string(5, 11)',
  email: '@string(5, 100)',
  address: '@string(5, 200)',
  avatar: '@string(5, 255)',
  enrollmentDate: '@date',
  status: '@string(5, 20)',
  remark: '@string(10)',
  createdAt: '@datetime',
  updatedAt: '@datetime',
};

// 模拟数据存储
let mockData = Mock.mock({
  'items|100': [mockTemplate]
}).items;

// 获取学生列表
export const getMockStudentData = (pageNumber: number, pageSize: number) => {
  const start = (pageNumber - 1) * pageSize;
  const records = mockData.slice(start, start + pageSize);

  return Promise.resolve({
    msg: "SUCCESS",
    code: 0,
    data: {
      current: pageNumber,
      total: mockData.length,
      pages: Math.ceil(mockData.length / pageSize),
      size: pageSize,
      records
    }
  });
};

// 添加学生
export const mockAddStudent = (entity: Partial<Student>) => {
  const now = new Date().toISOString();
  const newStudent = {
    ...entity,
    id: mockData.length + 1,
    createdAt: now,
    updatedAt: now
  };
  mockData.push(newStudent);
  return Promise.resolve({ code: 0, msg: 'Student added successfully' });
};

// 编辑学生
export const mockEditStudent = (id: any, valueEntity: Partial<Student>) => {
  const now = new Date().toISOString();
  mockData = mockData.map(item =>
    item.id === id ? { ...item, ...valueEntity, updatedAt: now } : item
  );
  return Promise.resolve({ code: 0, msg: 'Student updated successfully' });
};

// 删除学生
export const mockDeleteStudent = (id: any) => {
  mockData = mockData.filter(item => item.id !== id);
  return Promise.resolve({ code: 0, msg: 'Student deleted successfully' });
};

// 获取学生详情
export const mockFetchStudentDetail = (id: any) => {
  const record = mockData.find(item => item.id === id);
  if (record) {
    return Promise.resolve({ code: 0, data: record });
  }
  return Promise.reject(new Error('Student not found'));
};